package cn.minimal;

public class Shot implements Runnable {
    int x = 0;
    int y = 0;
    int direction = 0;
    int speed = 3;
    boolean isLive = true;
    public boolean isLive() {
        return isLive;
    }
    public void setLive(boolean live) {
        isLive = live;
    }
    public Shot(int x, int y, int direction) {
        if(direction == 0) {
            this.x = x + 10;
            this.y = y;
        } else if(direction == 1) {
            this.x = x + 30;
            this.y = y + 10;
        } else if(direction == 2) {
            this.x = x + 10;
            this.y = y + 30;
        } else if(direction == 3) {
            this.x = x;
            this.y = y + 10;
        }
        this.direction = direction;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getDirection() {
        return direction;
    }
    public void setDirection(int direction) {
        this.direction = direction;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    @Override
    public void run() {
        while(isLive) {
            try {
                Thread.sleep(20);
            } catch(InterruptedException e) {}

            if(direction == 0) {  // 0 - 方向向上
                this.setY(y - speed);
                if(this.getY() < 0) {
                    this.isLive = false;
                    break;
                }
            }
            if(direction == 1) { // 1 - 方向向右
                this.setX(x + speed);
                if(this.getX() > 800) {
                    this.isLive = false;
                    break;
                }
            }
            if(direction == 2) { // 2 - 方向向下
                this.setY(y + speed);
                if(this.getY() > 600) {
                    this.isLive = false;
                    break;
                }
            }
            if(direction == 3) { // 3 - 方向向左
                this.setX(x - speed);
                if(this.getX() < 0) {
                    this.isLive = false;
                    break;
                }
            }
        }
    }
}
